﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using DAL;

namespace WebAPI.Controllers
{
    public class TypeEvenementController : ApiController
    {
        private MyTeamEntities db = new MyTeamEntities();

        // GET api/TypeEvenement
        public IQueryable<TypeEvenement> GetTypeEvenements()
        {
            return db.TypeEvenements;
        }

        // GET api/TypeEvenement/5
        [ResponseType(typeof(TypeEvenement))]
        public IHttpActionResult GetTypeEvenement(short id)
        {
            TypeEvenement typeevenement = db.TypeEvenements.Find(id);
            if (typeevenement == null)
            {
                return NotFound();
            }

            return Ok(typeevenement);
        }

        // PUT api/TypeEvenement/5
        public IHttpActionResult PutTypeEvenement(short id, TypeEvenement typeevenement)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != typeevenement.Typ_id)
            {
                return BadRequest();
            }

            db.Entry(typeevenement).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TypeEvenementExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/TypeEvenement
        [ResponseType(typeof(TypeEvenement))]
        public IHttpActionResult PostTypeEvenement(TypeEvenement typeevenement)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.TypeEvenements.Add(typeevenement);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = typeevenement.Typ_id }, typeevenement);
        }

        // DELETE api/TypeEvenement/5
        [ResponseType(typeof(TypeEvenement))]
        public IHttpActionResult DeleteTypeEvenement(short id)
        {
            TypeEvenement typeevenement = db.TypeEvenements.Find(id);
            if (typeevenement == null)
            {
                return NotFound();
            }

            db.TypeEvenements.Remove(typeevenement);
            db.SaveChanges();

            return Ok(typeevenement);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool TypeEvenementExists(short id)
        {
            return db.TypeEvenements.Count(e => e.Typ_id == id) > 0;
        }
    }
}